package com.webull.information.center.carwler.dao;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.webull.information.center.carwler.common.model.hs.MfNews;
import com.webull.information.center.carwler.common.model.hs.parm.PageParm;
import com.webull.information.center.dao.JdbcHelper;

/**
 * 恒生新闻dao
 *
 * @author shimingjun
 * @version 1.0
 * @date 2016年8月22日 下午2:01:08
 * @since JDK 1.8
 */
@Repository
public class HangsengNewsDao {
    private static final RowMapper<MfNews> mfNewsRM = BeanPropertyRowMapper.newInstance(MfNews.class);
    private final Logger logger = LogManager.getLogger(getClass());
    private @Qualifier("hs_jdbcTemplate")
    @Autowired(required = false)
    JdbcTemplate jdbcTemplate;

    private @Qualifier("hs_npJdbcTemplate")
    @Autowired(required = false)
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public List<MfNews> pageFundMfNews(PageParm pageParm) {
        logger.info("sql:{},{}", Sql.PAGE_MF_NEWS, JdbcHelper.getSqlParameterSource(pageParm));
        return namedParameterJdbcTemplate.query(Sql.PAGE_MF_NEWS, JdbcHelper.getSqlParameterSource(pageParm), mfNewsRM);
    }

    public List<MfNews> pageFundAnalyReport(PageParm pageParm) {
        logger.info("sql:{},{}", Sql.PAGE_MF_NEWS, JdbcHelper.getSqlParameterSource(pageParm));
        return namedParameterJdbcTemplate.query(Sql.PAGE_MF_ANALYSEREPORT, JdbcHelper.getSqlParameterSource(pageParm),
                mfNewsRM);
    }

    private class Sql {
        private static final String PAGE_MF_NEWS = "SELECT c.SecuCode,c.SecuAbbr,a.*  FROM MF_News a"
                + " LEFT JOIN MF_News_SE b ON a.ID=b.ID" + " LEFT JOIN  SecuMain c ON c.InnerCode=b.Code"
                + " WHERE b.TypeCode=1 AND a.InfoPublDate > :startDate and a.InfoPublDate< :endDate  limit :start,:pageSize";
        // "select * from mf_news where InfoPublDate > :startDate and
        // InfoPublDate< :endDate limit :start,:pageSize";
        private static final String PAGE_MF_ANALYSEREPORT = "SELECT c.SecuCode,c.SecuAbbr,a.*  FROM MF_AnalyseReport a"
                + " LEFT JOIN MF_AnalyseReport_SE b ON a.ID=b.ID" + " LEFT JOIN  SecuMain c ON c.InnerCode=b.Code"
                + " WHERE b.TypeCode=1 AND a.InfoPublDate > :startDate and a.InfoPublDate< :endDate  limit :start,:pageSize";

        // "select * from MF_AnalyseReport where InfoPublDate > :startDate and
        // InfoPublDate< :endDate limit :start,:pageSize";
    }
}
